jQuery.easing.def = "easeOutExpo";

var selectedSectionID;
var currentMenuPositionID;
var isMenuOpen = true;
var isMenuTweening = false;

$(document).ready(function() {

  var inAnimation;
  var z = 1;
  var activeSlides=new Array();
  var totalSlides=new Array();
  
  $('dl').each(function(index) {
  		
		activeSlides[index] = 0;
		totalSlides[index] = 0;
		
		$(this).parent().find('.arrow-right').bind('click', function() {
			slide(index, activeSlides[index]+1);
		});
		
		$(this).parent().find('.arrow-left').bind('click', function() {
			slide(index, activeSlides[index]-1);
		});
		
		var totImg = $(this).find('dd img').size();
		
  		$(this).find('dd img').each(function(index2) {
  		
  			totalSlides[index]++;
  			z++; //at the end we have the highest z-index value stored in the z variable
    		
    		
    		$(this).parent().css('z-index', z);
    
    		var title = $(this).attr('title');
    		
    		if(title !== false && totImg > 1)
    		{
    			var elem = $(this).parent();
    			$(this).parent().parent().append('<dt><p>'+title+'</p></dt>');
    			
    			$(this).parent().parent().find('dt').eq(index2).bind('click', function() {
  					slide(index, index2);
				});
    			
    			
    		}
    	});
    	
    	checkArrowsVisibility(index);
		activateButton(index);
		
  	});

	function slide(subID, slideID)
	{
		
		if(slideID != activeSlides[subID])
		{

			if(slideID >= 0 && slideID <= totalSlides[subID])
			{
				var elem = $('dl').eq(subID).find('dd').eq(slideID);
				// swap the index
				z++;
				$(elem).css('z-index', z);
				
				var startPos;
				
				// if the slide index is major make the left - right transition
				if(slideID > activeSlides[subID])
				{
					startPos = '960px';
		  		}else{
					startPos = '-960px';
		  		}
		  		
		  		$(elem).css('left', startPos);
	
				$(elem).animate({
		    		left: '0'
		  			}, 1000,  function() {
		    			// Animation complete.
		  		});
	
		  		activeSlides[subID] = slideID;
		  		activateButton(subID);
	  		}
	  		
	  		checkArrowsVisibility(subID);
	  	}
	}
	
	function checkArrowsVisibility(subID)
	{
		if(totalSlides[subID] > 1)
		{
		
			if(activeSlides[subID] < 1)
			{
				$('.arrow-left').eq(subID).hide('fast');
			}else{
				$('.arrow-left').eq(subID).show('fast');
			}
			
			if(activeSlides[subID] >= totalSlides[subID]-1)
			{
				$('.arrow-right').eq(subID).hide('fast');
			}else{
				$('.arrow-right').eq(subID).show('fast');
			}
		}else{
			$('.arrow-left').eq(subID).hide();
			$('.arrow-right').eq(subID).hide();
		}
		
		
	}
	
	function activateButton(subID)
	{
	
		var elem = $('dl').eq(subID).find('dd').eq(activeSlides[subID]);
		z++;
		$(elem).css('z-index', z);


		$('dl').eq(subID).find('dt').each(function(index) {
  			
  			if(index == activeSlides[subID])
  			{
  				$(this).css('background-color', 'white');
  			}else{
  				$(this).css('background-color', '#EEEEEE');
  			}
  			
		});
	}
	
	
	
	$('.top').each(function(index) {
		$(this).bind('click', function() {
  			$.scrollTo(0,{duration:1400, easing:'easeInOutExpo'} );
		});
	});
	
	adjustImages();
  	setTimeout(menuOut, 3000);
	
	
	$(window).scroll(function () { 

		 positionMenuToOffset();
	    
    
	}); 
	
	
  	 positionMenuToOffset();
});

$(window).resize(function() {
	adjustImages();
});

function adjustImages() {

	
	var newW = $(window).width();
	if(newW >= 960)
  	{
  		newW = 960;
  	}
  	// 960 : 480 = ww : x 
  	var newH = newW*480/960;
	
	$('.aircraft-sub').each(function(index) {
	
		$(this).height(newH+200);
		
		$(this).find('dt').each(function(index2) {
			$(this).css('top', newH);
		});
		
		$(this).find('dl dd .attachment-full').each(function(index3) {
				$(this).width(newW);
				$(this).height(newH);

	 		});
		});
  	
};



function scrollTo(index)
{
	$.scrollTo($('.aircraft-sub').eq(index),{duration:1400, easing:'easeInOutExpo'} );	
	setTimeout(positionMenuToOffset, 500);
}




function menuOver(){

	if(isMenuOpen == false)
	{
		
		$('#close').show('fast');
		$('#arrowMenu').hide('fast');
		
		isMenuTweening = true;
		$('#sideMenu').animate({
			right: 0
		
			}, 500, 'easeOutExpo', function() {
				// Animation complete.
				isMenuOpen = true;
				isMenuTweening = false;
		});
		
	}
}

function menuOut(){
	
	if(isMenuOpen == true)
	{
		
		$('#close').hide('fast');
		$('#arrowMenu').show('fast');
		
		isMenuTweening = true;
		$('#sideMenu').animate({
			right: -150
			}, 500, 'easeInExpo', function() {
				// Animation complete.
				isMenuOpen = false;
				isMenuTweening = false;
		});
		
	}
}



function positionMenuToOffset()
{
	selectedSectionID = findSection();
	
	//console.log(selectedSectionID);
		
	if(selectedSectionID != currentMenuPositionID)
	{
			
		currentMenuPositionID = selectedSectionID;
			
		$('#sideMenu').animate({
			top: $('.aircraft-sub').eq(selectedSectionID).offset().top
			}, { duration: 1500, queue: false, easing:'easeOutExpo' }, function() {
				// Animation complete.				
		});
	}
}

function findSection()
{
	var id;
	
	$('.aircraft-sub').each(function(index) {
	
		//console.log($(document).scrollTop() +" "+$(this).offset().top);
		
		if(($(document).scrollTop()) < ($(this).offset().top+100))
		{
			id = index;
			return false;
		}
		
	});
	return id;
}

